import { Schema } from 'mongoose';
import { IAttachment } from '../types/IAttachment';

export const AttachmentSchema = new Schema<IAttachment>({
  attachmentId: { type: String, required: true, unique: true, index: true },
  filename: { type: String, required: true },
  originalName: { type: String, required: true },
  mimeType: { type: String, required: true, index: true },
  size: { type: Number, required: true, min: 0 },
  path: { type: String, required: true },
  hash: { type: String, required: true, index: true },
  uploadedBy: { type: String, required: true, index: true },
  sessionId: { type: String, sparse: true, index: true },
  messageId: { type: String, sparse: true, index: true },
  metadata: {
    width: { type: Number, min: 0 },
    height: { type: Number, min: 0 },
    duration: { type: Number, min: 0 },
    encoding: { type: String }
  },
  status: {
    type: String,
    enum: ['uploading', 'processing', 'ready', 'error'],
    default: 'uploading',
    index: true
  },
  deletedAt: { type: Date, sparse: true }
}, {
  timestamps: true,
  collection: 'attachments'
});

// Compound Indexes
AttachmentSchema.index({ uploadedBy: 1, createdAt: -1 });
AttachmentSchema.index({ mimeType: 1, status: 1 });